layui.use(['table', 'jquery','element'], function(){
    var $ = layui.jquery;
    window.$=$;
    var element = layui.element;

    var log = function (logtext) {
        var str = $('#log').val();
        $('#log').val(str  + logtext + '\n');
    };
    var log_clear=function () {
        $('#log').val('');
    };
    var check = function(){
        if($('#phone').val() ==='' && $('#idno').val() ==='' && $('#name').val() ==='' && $('#crfuid').val() ===''){
            return false;
        }
        return true;
    };
    var commonSubmit=function(fields,callback){
        if(check() == false){
            layer.msg('请至少输入一个查询条件', {icon: 5});
            return;
        }
        var form_data = $("#form_gen_data").serialize()+ '&openid='+ parent.window.openid + '&env_tools='+parent.window.env_tools
        if(fields.param){
            form_data = form_data  + '&' + fields.param
        }
        $.ajax({
            type: 'post',
            url: fields.url,
            data: form_data,
            success: function(data) {
                if(data.code ==1){
                    if(typeof (callback) == "function"){
                        callback(data)
                    }
                }else {
                    layer.msg(data.message);
                }

            }
        });
    };
    $('#query_thumb').on('click', function(){
        commonSubmit({
            url:'/tools/query_thumb',
            param:''
        },function (data) {
            log_clear();
            log(
                "手机号：" + data.data.phone + "   "+
                "姓名：  " + data.data.name + "   "+
                "身份证： " + data.data.idno + "   "+
                "crfuid： " + data.data.crfuid + "   "+
                "渠道： " + data.data.crf_customer_basic_info.channel_no + "   "+
                "分表：  " + data.data.table_prefix
            );
            $('#extra_info').html('<textarea placeholder="" id="log" class="layui-textarea" name="desc">' + JSON.stringify(data) + '</textarea>');

        });
    });
    $('#query_limit').on('click', function(){
        commonSubmit({
            url:'/tools/query_limit',
            param:''
        },function (data) {
            log_clear();
            log(
                "手机号：" + data.data.phone + "   "+
                "姓名：  " + data.data.name + "   "+
                "身份证： " + data.data.idno + "   "+
                "crfuid： " + data.data.crfuid + "   "+
                "渠道： " + data.data.crf_customer_basic_info.channel_no + "   "+
                "分表：  " + data.data.table_prefix
            );
            for(i in data.data.crf_limit){
                log(
                    "额度=》partner_id：" + data.data.crf_limit[i].partner_id + "   "+
                    "借款额度：  " + data.data.crf_limit[i].loan_limit + "   "+
                    "冻结额度： " + data.data.crf_limit[i].frozen_limit + "   "+
                    "已用额度： " + data.data.crf_limit[i].used_limit + "   "+
                    "状态： " + data.data.crf_limit[i].status
                );
            }
            for(i in data.data.crf_limit_detail){
                log(
                    "额度明细=》partner_id：" + data.data.crf_limit_detail[i].partner_id + "   "+
                    "额度类型：  " + data.data.crf_limit_detail[i].limit_type + "   "+
                    "借款额度： " + data.data.crf_limit_detail[i].loan_limit + "   "+
                    "状态： " + data.data.crf_limit_detail[i].status
                );
            }


            $('#extra_info').html('<textarea placeholder="" id="log" class="layui-textarea" name="desc">' + JSON.stringify(data) + '</textarea>');

        });
    });

    $('#query_member').on('click', function(){
        commonSubmit({
            url:'/tools/query_member',
            param:''
        },function (data) {
            log_clear();
            log(
                "手机号：" + data.data.phone + "   "+
                "姓名：  " + data.data.name + "   "+
                "身份证： " + data.data.idno + "   "+
                "crfuid： " + data.data.crfuid + "   "+
                "渠道： " + data.data.crf_customer_basic_info.channel_no + "   "+
                "分表：  " + data.data.table_prefix
            );
            if(data.data.crf_member_info_xfd){
                console.log(data);
                log(
                    "消费贷会员信息=》会员类型：" + data.data.crf_member_info_xfd.member_type + "   "+
                    "会员名称：  " + data.data.crf_member_info_xfd.member_name + "   "+
                    "会员金额： " + data.data.crf_member_info_xfd.amount + "   "+
                    "开始日期： " + data.data.crf_member_info_xfd.start_date + "   "+
                    "结束日期： " + data.data.crf_member_info_xfd.end_date + "   "+
                    "产品编号： " + data.data.crf_member_info_xfd.product_no
                );

            }
            if(data.data.crf_qshd_member_info){
                log(
                    "轻生活贷会员信息=》会员类型：" + data.data.crf_qshd_member_info.member_type + "   "+
                    "会员名称：  " + data.data.crf_qshd_member_info.member_name + "   "+
                    "会员金额： " + data.data.crf_qshd_member_info.amount + "   "+
                    "开始日期： " + data.data.crf_qshd_member_info.start_date + "   "+
                    "结束日期： " + data.data.crf_qshd_member_info.end_date + "   "+
                    "产品编号： " + data.data.crf_qshd_member_info.product_no
                );

            }


            $('#extra_info').html('<textarea placeholder="" id="log" class="layui-textarea" name="desc">' + JSON.stringify(data) + '</textarea>');

        });
    });


    $('#query_account').on('click', function(){
        commonSubmit({
            url:'/tools/query_account',
            param:''
        },function (data) {
            log_clear();
            log(
                "手机号：" + data.data.phone + "   "+
                "姓名：  " + data.data.name + "   "+
                "身份证： " + data.data.idno + "   "+
                "crfuid： " + data.data.crfuid + "   "+
                "渠道： " + data.data.crf_customer_basic_info.channel_no + "   "+
                "分表：  " + data.data.table_prefix
            );
            if(data.data.crf_account){
                log(
                    "账户信息=》kisso_id：" + data.data.crf_account.kisso_id + "   "+
                    "账户：  " + data.data.crf_account.account + "   "+
                    "密码： " + data.data.crf_account.password + "   "+
                    "开户来源系统： " + data.data.crf_account.from_system + "   "+
                    "开户日期： " + data.data.crf_account.create_time + "   "+
                    "状态： " + data.data.crf_account.status
                );

            }
            if(data.data.acc_fuiou_virtual_account_info){
                log(
                    "富有开户信息=》fts账号：" + data.data.acc_fuiou_virtual_account_info.account_no + "   "+
                    "富有账号：  " + data.data.acc_fuiou_virtual_account_info.external_account_no + "   "+
                    "绑卡卡号： " + data.data.acc_fuiou_virtual_account_info.bank_card_no + "   "+
                    "银行编码： " + data.data.acc_fuiou_virtual_account_info.bank_code + "   "+
                    "富有签约状态： " + data.data.acc_fuiou_virtual_account_info.is_signed + "   "+
                    "富有开户日期： " + data.data.acc_fuiou_virtual_account_info.created_date
                );

            }


            $('#extra_info').html('<textarea placeholder="" id="log" class="layui-textarea" name="desc">' + JSON.stringify(data) + '</textarea>');

        });
    });
    function business_noextra(business,extra=''){
        param = 'business='+business
        if(extra != ''){
            param = param + '&' + extra
        }
        commonSubmit({
            url:'/tools/common_manage',
            param:param
        },function (data) {
            log_clear();
            log(data.log.replace(/\\n/g,'\n'));
            $('#extra_info').html('<textarea placeholder="" id="log" class="layui-textarea" name="desc">' + JSON.stringify(data) + '</textarea>');
            layer.alert(data.message);
        });
    };

    $('.common_business').on('click', function(){
        business = $(this).data('type');
        is_extra = $(this).data('extra');
        if (is_extra != 1 ){
            business_noextra(business);
        }else{
            layer.open({
              type: 2,
              area: ['700px', '450px'],
              fixed: false, //不固定
              maxmin: true,
              content: '/tools/common_extras?business='+business
            });
        }


    });
    $('#query_extra').on('click', function(){
        business_noextra(business,$(this).data('value'));
    });



});